import os
'''索引文件'''
# 定义文件路径
file1_path = 'name31_.txt'  # 索引文件
file2_path = 'E:\data\data_09053/room/associate.txt'  # 用索引查找的文件
output_path = 'n3.txt'   # 输出文件的路径

# 读取第一个文件的内容
with open(file1_path, 'r') as f1:
    file1_names = set(line.strip() for line in f1.readlines())

# 读取第二个文件的内容
matched_lines = []
with open(file2_path, 'r') as f2:
    for line in f2:
        # 提取该行中的rgb图像文件名
        rgb_part = line.split()[1]  # 第二个部分是rgb图像路径
        rgb_name = os.path.basename(rgb_part)  # 获取rgb图像的文件名
        if rgb_name in file1_names:
            matched_lines.append(line)

# 将匹配的行写入nnn.txt文件
with open(output_path, 'w') as output_file:
    for matched_line in matched_lines:
        output_file.write(matched_line)

print(f"成功将匹配的行写入 {output_path} 文件中。")
